Skip to content

feat(agent): route selected products to the ai-gateway - #3659

Open
brandonleung wants to merge 3 commits into
mainfrom
brandon/sandbox-ai-gateway
Open

feat(agent): route selected products to the ai-gateway#3659
brandonleung wants to merge 3 commits into
mainfrom
brandon/sandbox-ai-gateway

Conversation

@brandonleung

@brandonleung brandonleung commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Problem

Model calls from code sandboxes all go through the legacy Python llm-gateway. We're migrating products to the new Go ai-gateway one at a time, so the agent-server needs to pick a gateway per request. The two gateways also take attribution differently: the old one wants a product slug in the URL plus per-property headers, the new one wants a single JSON header.

Changes

Routing to the new gateway is opt-in via two env vars:

  • AI_GATEWAY_URL is the new gateway's address; AI_GATEWAY_PRODUCTS lists the ai_product values that may use it. Both must be set. Unlisted products keep using the Python gateway.
  • Requests on the new path carry ai_product, ai_stage, and team_id in one X-PostHog-Properties header, built by a new buildPosthogPropertiesBlob helper in @posthog/shared (enforces the gateway's 8KB / ASCII limits).
  • Signals sandbox stages get per-stage ai_product tags (signals_scout, ...), matching the non-sandbox signals stages.

How did you test this?

Unit tests cover the routing choice and the header builder (cap trimming, sanitization, unlisted products falling back). tsc and biome clean.

Inert until the sandbox chart sets both env vars (PostHog/posthog#72770 plus a charts PR). Rollback: clear either one.

@trunk-io

trunk-io Bot commented Jul 21, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 8d94179.

@brandonleung
brandonleung marked this pull request as ready for review July 22, 2026 03:02
@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(agent): route selected products to ..." | Re-trigger Greptile

@brandonleung
brandonleung requested a review from a team July 22, 2026 03:06

@richardsolomou richardsolomou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be worth testing exact 8,192-byte and 8,193-byte blobs so an off-by-one at the gateway limit cant slip through

Comment thread packages/agent/src/utils/gateway.ts Outdated
@posthog

posthog Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

👋 Visual changes detected for this PR.

Review and approve in PostHog Visual Review

If these changes are unexpected, they may be caused by a flaky test or a broken snapshot on master. Don't approve — rerun the job or wait for a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants